#region Imported Namespaces


using System;
using System.Runtime.InteropServices;

using WtfManager.WindowsNative.Shell32;


#endregion


namespace WtfManager.WindowsNative.User32
{
  /// <summary>
  /// Wraps necessary functions imported from User32.dll. Code courtesy of MSDN Cold Rooster Consulting example.
  /// </summary>
  public class User32Methods
  {
    #region Fields


    public static Int32 GWL_EXSTYLE = -20;
    public static Int32 WS_EX_LAYERED = 0x00080000;
    public static Int32 WS_EX_TRANSPARENT = 0x00000020;


    #endregion


    #region Methods


    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern bool GetCursorPos(out POINT pt);


    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern Int32 GetWindowLong(IntPtr hWnd, Int32 nIndex);


    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern Int32 SetWindowLong(IntPtr hWnd, Int32 nIndex, Int32 newVal);


    /// <summary>
    /// Provides access to function required to delete handle. This method is used internally
    /// and is not required to be called separately.
    /// </summary>
    /// <param name="hIcon">Pointer to icon handle.</param>
    /// <returns>N/A</returns>
    [DllImport("User32.dll")]
    public static extern int DestroyIcon(IntPtr hIcon);


    // The CreatePopupMenu function creates a drop-down menu, submenu, or shortcut menu. The menu is initially empty. You can insert or append menu items by using the InsertMenuItem function. You can also use the InsertMenu function to insert menu items and the AppendMenu function to append menu items.
    [DllImport("user32", SetLastError = true, CharSet = CharSet.Auto)]
    public static extern IntPtr CreatePopupMenu();


    [DllImport("user32", SetLastError = true, CharSet = CharSet.Auto)]
    public static extern int GetMenuItemInfo(IntPtr hMenu, uint uItem, bool fByPosition, ref MENUITEMINFO lpmii);


    [DllImport("user32", SetLastError = true, CharSet = CharSet.Auto)]
    public static extern int GetMenuItemCount(IntPtr hMenu);


    // The DestroyMenu function destroys the specified menu and frees any memory that the menu occupies.
    [DllImport("user32", SetLastError = true, CharSet = CharSet.Auto)]
    public static extern bool DestroyMenu(IntPtr hMenu);


    // The TrackPopupMenuEx function displays a shortcut menu at the specified location and tracks the selection of items on the shortcut menu. The shortcut menu can appear anywhere on the screen.
    [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
    public static extern uint TrackPopupMenuEx(IntPtr hmenu, TPM flags, int x, int y, IntPtr hwnd, IntPtr lptpm);


    //This is the Import for the SetWindowsHookEx function.
    //Use this function to install a thread-specific hook.
    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);


    //This is the Import for the UnhookWindowsHookEx function.
    //Call this function to uninstall the hook.
    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    public static extern bool UnhookWindowsHookEx(int idHook);


    //This is the Import for the CallNextHookEx function.
    //Use this function to pass the hook information to the next hook procedure in chain.
    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);


    #endregion
  }


  public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
}